package com.weetop.aklyscan.utils;

import android.app.Activity;
import android.content.Context;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.load.resource.bitmap.CenterCrop;
import com.bumptech.glide.load.resource.bitmap.CircleCrop;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.weetop.aklyscan.base.BaseUrl;

import static com.weetop.aklyscan.utils.BaseUtils.dip2px;


/**
 * Glide  加载图片框架
 * Created by yzhya on 2018/10/17.
 */

public class GlideUtil {

    public static void load(Context context, ImageView imgView, String url) {

        if (url != null)
            if (context != null) {
                if (!url.contains("http")) {
                    url = BaseUrl.url1 + url;
                }
                if (url.contains("/public"))
                    url = url.replace("/public", "");
                Glide.with(context)
                        .load(url)
                        .apply(RequestOptions.diskCacheStrategyOf(DiskCacheStrategy.AUTOMATIC))
                        .apply(new RequestOptions().transforms(new CenterCrop(), new RoundedCorners(4)))
                        .into(imgView);
            }
    }

    public static void load(Activity context, ImageView imgView, String url) {
        CornerTransform transformation = new CornerTransform(context, dip2px(context, 4));
        transformation.setExceptCorner(false, false, false, false);
        if (url != null)
            if (!context.isDestroyed())
                Glide.with(context)
                        .load(url)
                        .apply(RequestOptions.diskCacheStrategyOf(DiskCacheStrategy.AUTOMATIC))
                        .apply(new RequestOptions().transforms(new CenterCrop(), new RoundedCorners(4)))
                        .into(imgView);
    }

    public static void load(Context context, ImageView imgView, String url, int placeholderImg) {
        CornerTransform transformation = new CornerTransform(context, dip2px(context, 4));
        transformation.setExceptCorner(false, false, false, false);
        if (url != null)
            if (context != null)
                Glide.with(context)
                        .load(url)
                        .apply(RequestOptions.diskCacheStrategyOf(DiskCacheStrategy.AUTOMATIC))
                        .apply(new RequestOptions().transforms(new CenterCrop(), new RoundedCorners(4)))
                        .into(imgView);
    }

    public static void load(Activity context, ImageView imgView, String url, int placeholderImg) {
        CornerTransform transformation = new CornerTransform(context, dip2px(context, 4));
        transformation.setExceptCorner(false, false, false, false);
        if (url != null)
            if (!context.isDestroyed())
                Glide.with(context)
                        .load(url)
                        .apply(RequestOptions.diskCacheStrategyOf(DiskCacheStrategy.AUTOMATIC))
                        .apply(new RequestOptions().transforms(new CenterCrop(), new RoundedCorners(4)).placeholder(placeholderImg))
                        .into(imgView);
    }


    /**
     * @param context        上下文
     * @param imgView        imageView
     * @param url            图片地址
     * @param placeholderImg 默认占位图  -1：无占位图
     * @param errorImg       加载失败占位图
     */
    public static void load(Context context, ImageView imgView, String url, int placeholderImg, int errorImg) {
        CornerTransform transformation = new CornerTransform(context, dip2px(context, 4));
        transformation.setExceptCorner(false, false, false, false);

        if (url != null)
            if (context != null)
                if (placeholderImg != -1) {
                    Glide.with(context)
                            .load(url)
                            .apply(RequestOptions.diskCacheStrategyOf(DiskCacheStrategy.AUTOMATIC))
                            .apply(new RequestOptions()
                                    .transforms(new CenterCrop(), new RoundedCorners(4))
                                    .placeholder(placeholderImg)//图片加载出来前，显示的图片
                                    .fallback(errorImg)
                                    .error(errorImg))
                            .into(imgView);
                } else {
                    Glide.with(context)
                            .load(url)
                            .apply(RequestOptions.diskCacheStrategyOf(DiskCacheStrategy.AUTOMATIC))
                            .apply(new RequestOptions()
                                    .transforms(new CenterCrop(), new RoundedCorners(4))
                                    .fallback(errorImg)
                                    .error(errorImg))
                            .into(imgView);
                }
    }

    public static void load(Activity context, ImageView imgView, String url, int placeholderImg, int errorImg) {
        CornerTransform transformation = new CornerTransform(context, dip2px(context, 4));
        transformation.setExceptCorner(false, false, false, false);
        if (url != null)
            if (!context.isDestroyed())
                if (placeholderImg != -1) {
                    Glide.with(context)
                            .load(url)
                            .apply(RequestOptions.diskCacheStrategyOf(DiskCacheStrategy.AUTOMATIC))
                            .apply(new RequestOptions()
                                    .transforms(new CenterCrop(), new RoundedCorners(4))
                                    .placeholder(placeholderImg)//图片加载出来前，显示的图片
                                    .fallback(errorImg)
                                    .error(errorImg))
                            .into(imgView);
                } else {
                    Glide.with(context)
                            .load(url)
                            .apply(RequestOptions.diskCacheStrategyOf(DiskCacheStrategy.AUTOMATIC))
                            .apply(new RequestOptions()
                                    .transforms(new CenterCrop(), new RoundedCorners(4))
                                    .fallback(errorImg)
                                    .error(errorImg))
                            .into(imgView);
                }
    }


    public static void load(Activity context, ImageView imgView, String url, int placeholderImg, int errorImg, boolean iscircle) {
        if (url != null)
            if (!context.isDestroyed())
                if (placeholderImg != -1) {
                    Glide.with(context)
                            .load(url)
                            .apply(RequestOptions.diskCacheStrategyOf(DiskCacheStrategy.AUTOMATIC))
                            .apply(new RequestOptions()
                                    .transforms(new CenterCrop(), new CircleCrop())
                                    .placeholder(placeholderImg)//图片加载出来前，显示的图片
                                    .fallback(errorImg)
                                    .error(errorImg))
                            .into(imgView);
                } else {
                    Glide.with(context)
                            .load(url)
                            .apply(RequestOptions.diskCacheStrategyOf(DiskCacheStrategy.AUTOMATIC))
                            .apply(new RequestOptions()
                                    .transforms(new CenterCrop(), new RoundedCorners(4))
                                    .fallback(errorImg)
                                    .error(errorImg))
                            .into(imgView);
                }
    }

}
